import InteractionUtil from '../../shell/ui/interaction';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    msg: '获取验证码',
    phone: '',
    cdoe: '',
    password: '',
    confirmPwd: '',
    isLoading: false,
    isSubmit: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getApp().getSaleMode()
  },
  submitHandler(e) {
    const {
      password,
      confirmPwd,
      phone,
      code
    } = this.data
    if (phone.trim() === '') {
      InteractionUtil.showNotice('请输入手机号')
      return
    }
    if (code.trim() === '') {
      InteractionUtil.showNotice('请输入验证码')
      return
    }
    if (password.trim() === '') {
      InteractionUtil.showNotice('请输入密码')
      return
    }
    if (confirmPwd.trim() === '' || password.trim() !== confirmPwd.trim()) {
      InteractionUtil.showNotice('两次输入密码不一致')
      return
    }
    const {
      isSubmit
    } = this.data
    if (isSubmit) return
    this.setData({
      isSubmit: true
    })
    wx.$http().request({
      url: '/AppMaster/Login/checkPwdMessageCode',
      method: 'POST',
      data: {
        phone,
        code
      },
      success: res => {
        const {
          data
        } = res
        const {
          result,
          msg
        } = data
        if (result !== 0) {
          InteractionUtil.showNotice(msg)
          this.setData({
            isSubmit: false
          })
        } else {
          const {
            sign
          } = data.data
          wx.$http().request({
            url: '/AppMaster/Login/resetPwdCode',
            data: {
              phone,
              sign,
              password,
              confirmPassword: confirmPwd
            },
            method: 'POST',
            success: res => {
              this.setData({
                isSubmit: false
              })
              const {
                data
              } = res
              const {
                result,
                msg
              } = data
              if (result * 1 === 0) {
                InteractionUtil.showSuccess('修改成功!')
                wx.navigateBack()
              } else {
                InteractionUtil.showNotice(msg)
                this.setData({
                  isSubmit: false
                })
              }
            }
          })
        }
      }
    })
  },
  getCode() {
    const {
      phone,
      isLoading
    } = this.data
    if (isLoading) return
    if (!(/^1[3456789]\d{9}$/.test(phone))) {
      InteractionUtil.showNotice('请输入正确的手机号码')
      return
    }
    this.setData({
      isLoading: true
    })
    wx.$http().request({
      url: '/AppMaster/Login/backPwdSendMessageCode',
      data: {
        phone
      },
      success: res => {
        const {
          result,
          msg
        } = res.data
        if (result * 1 === 0) {
          InteractionUtil.showSuccess('发送成功!')
          let time = 30
          const interval = setInterval(() => {
            if (time === 1) {
              clearInterval(interval)
              this.setData({
                msg: '获取验证码',
                isLoading: false
              })
              return
            }
            time -= 1
            const msg = time > 9 ? time : `0${time}`
            this.setData({
              msg: `重新获取(${msg})`
            })
          }, 1000);
        } else {
          InteractionUtil.showNotice(msg)
          this.setData({
            isLoading: false
          })
        }
      }
    })
  },
  phoneInput(e) {
    const {
      value
    } = e.detail
    this.setData({
      phone: value
    })
  },
  confirmInput(e) {
    const {
      value
    } = e.detail
    this.setData({
      confirmPwd: value
    })
  },
  codeInput(e) {
    const {
      value
    } = e.detail
    this.setData({
      code: value
    })
  },
  pwdInput(e) {
    const {
      value
    } = e.detail
    this.setData({
      password: value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})